package com.googlecode.chigs.web;

import java.util.List;

import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.image.Image;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.PropertyListView;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.Model;
import org.apache.wicket.request.target.basic.RedirectRequestTarget;
import org.apache.wicket.spring.injection.annot.SpringBean;

import brix.BrixNodeModel;
import brix.web.nodepage.BrixNodeWebPage;
import brix.web.nodepage.BrixPageParameters;

import com.googlecode.chigs.domain.Product;
import com.googlecode.chigs.repository.ChigsFacade;
import com.googlecode.chigscommons.repository.GenericDomainService;
import com.googlecode.chigscommons.web.common.IconFactory;

/**
 * @author the
 */
public class StatisticsPanel extends Panel {
	@SpringBean
	private ChigsFacade chigs;

	@SpringBean
	private GenericDomainService genericDomainService;
	
	public StatisticsPanel(String id) {
		super(id);
		List<Product> products = genericDomainService.loadAll(Product.class);
		add(new PropertyListView<Product>("product", products) {
			@Override
			protected void populateItem(ListItem<Product> item) {
				final Product product = item.getModelObject();

				item.add(new Label("name"));
				item.add(new Label("description"));
				Link<Void> modulesLink = new Link<Void>("modules.button") {
					@Override
					public void onClick() {
						BrixPageParameters params = new BrixPageParameters();
						params.addQueryParam("product", product.getId().toString());
						BrixNodeModel model = ((ChigsApplication) getApplication()).getI18nBrixLink(getSession(), "/documentation/modules.html");
						getRequestCycle().setRequestTarget(new RedirectRequestTarget(params.urlFor(new BrixNodeWebPage(model))));
					}
				};
				modulesLink.add(new Image("icon", IconFactory.createBoxIconReference()));
				modulesLink.add(new Label("modules", new Model<Long>(chigs.getModuleCountByProduct(product))));
				item.add(modulesLink);
				
				Link<Void> confLink = new Link<Void>("configurations.button") {
					@Override
					public void onClick() {
						BrixPageParameters params = new BrixPageParameters();
						params.addQueryParam("product", product.getId().toString());
						BrixNodeModel model = ((ChigsApplication) getApplication()).getI18nBrixLink(getSession(), "/configuration/configurations.html");
						getRequestCycle().setRequestTarget(new RedirectRequestTarget(params.urlFor(new BrixNodeWebPage(model))));
					}
				};
				confLink.add(new Image("icon", IconFactory.createGearIconReference()));
				confLink.add(new Label("configurations", new Model<Long>(chigs.getConfigurationCountByProduct(item.getModelObject()))));
				item.add(confLink);
			}
		});
	}
}

